home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
DDJMAG
/
DDJ9310.ZIP
/
1993-OCT.ZIP
/
BETA.ASC
< prev
next >
Wrap
Text File
|
1993-09-17
|
11KB
|
433 lines
_THE BETA PROGRAMMING LANGUAGE_
by Steve Mann
[LISTING ONE]
ORIGIN '~beta/macenv/v3.5/basicmacenv' (* part of the fragment system *)
--- program: descriptor ---(* for handling code modules *)
MacEnv (* include the basic app framework *)
(#
myWindow: @window (#
type::< windowTypes.zoom;
hasClose::< trueObject;
open::< (#
do (100, 100) -> position;
(300, 100) -> size;
My window' -> title;
#); #);
FileMenu::< (#
iNew::< (# hit::< (# do myWindow.open #); #);
iOpen::< (# hit::< (# do myWindow.open #); #);
#); #)
[LISTING TWO]
ORIGIN '~beta/macenv/v3.5/basicmacenv'
[[
--- INCLUDE '~beta/macenv/v3.5/fields'
--- INCLUDE '~beta/macenv/v3.5/macfile'
--- INCLUDE '~beta/basiclib/v1.3/file'
--- program:descriptor---
MacEnv (#
(* === Font Menu Patterns === *)
FontMenu: @Menu (#
fontSizes: [4] @integer;
firstFontOset: @integer;
theEditor: ^edWindow;
Name::< (# do 'Font' -> theName #);
(* create the font menu--names on top, sizes on bottom. the
* handler distinguishes between the two using the item number. *)
open::< (#
do System.AvailableFonts
(# do thisFontName[] -> Append #);
separator -> Append;
'9' -> Append -> firstFontOSet; '12' -> Append;
'14' -> Append; '18' -> Append;
9 -> fontSizes [1];12 -> fontSizes [2];
14 -> fontSizes [3];18 -> fontSizes [4];
#);
(* override the standard event handler for this menu
* to change the font or size of the current selection *)
EventHandler::< (#
EvalStatus::< (# Done::< trueObject #);
Select::< (#
do (if theEditor[] <> None // true then
(if theItem.ItemNumber >= firstFontOSet // true then
fontSizes [theItem.ItemNumber - firstFontOset + 1] ->
theEditor.ChangeSize;
else theItem.name -> theEditor.ChangeFont
if);if);
#); #); #);
(* === Editor Window Patterns === *)
edWindow: Window (#
Type::< WindowTypes.Zoom; HasClose::< TrueObject;
myText: ^StyledText;myFile: ^macfile;
opened: @boolean;
(* change the font type of the current text selection *)
ChangeFont: (#
aTextStyle: @TextStyle; start,end: @Integer
enter aTextStyle.name
do myEditor.Contents.Selection -> (start, end);
(start, end, aTextStyle.FontID) ->
myEditor.Contents.SetOneFont
#);
(* change the font size of the current text selection *)
ChangeSize: (#
fontSize, start,end: @Integer
enter fontSize
do myEditor.Contents.Selection -> (start, end);
(start, end, fontSize) ->
myEditor.Contents.SetOneSize
#);
(* create a dynamic menu item for the File/Close command *)
DoClose: @theFileMenu.action (#
hit::< (# do close #); #);
(* create a dynamic menu item for File/Save command *)
DoSave: @theFileMenu.action (#
hit::< (#
do (if opened // true then myEditor.saveMyEd
else myEditor.saveAsMyEd if)
#); #);
(* create a dynamic menu item for File/Save As command *)
DoSaveAs: @theFileMenu.action (#
hit::< (# do myEditor.saveAsMyEd #); #);
(* Window event handler--enable/disable the font menu and dynamic
* File commands as current window gets activated / deactivated *)
EventHandler::< (#
Activate::< (#
do this (edWindow)[] -> FontMenu.theEditor[];
FontMenu.enable;
DoClose[] -> theFileMenu.CloseItem.Attach;
DoSave[] -> theFileMenu.SaveItem.Attach;
DoSaveAs[] -> theFileMenu.SaveAsItem.Attach;
#);
Deactivate::< (#
do none -> FontMenu.theEditor[]; ;
FontMenu.disable;
theFileMenu.CloseItem.Detach;
theFileMenu.SaveItem.Detach;
theFileMenu.SaveAsItem.Detach;
#);#);
(* pattern to open an existing file *)
openFile: (#
do newFile;
(if ('','Select a File to Edit: ') ->
myFile.GetFile -> opened // true then
myFile.RestoreStyledText -> myText[]
if)
exit opened
#);
(* pattern to create a new file *)
newFile:(#
do &StyledText[] -> myText[];
&macfile[] -> myFile[]
#);
(* pattern to open a new window *)
open::< (#
do 'Untitled' -> title;
(if opened // true then
myFile.entry.path.name -> title
if);
nextPosition -> Position; (350,400) -> Size;
(5,10) -> nextPosition.add; myEditor.open
#);
(* === Text Editor Patterns--scoped inside Window to have ===
* === access to all of the Window attributes === *)
myEditor: @TextEditor (#
(* track text changes (to disable Save if no changes). *)
docChanged: @boolean;
ContentsDesc::< (#
Textchanged::< (# do true -> docChanged #);
#);
(* open (create) a new text editor--bind to window *)
open::< (#
do this(edWindow).Size->Size;
true->BindBottom->BindRight;
myText[] -> Contents.contents;
myEditor[] -> Target;
false -> docChanged
#);
(* save the current document to disk *)
saveMyEd: (#
do contents.contents -> myFile.SaveStyledText
#);
(* save the current doc to disk, but allow a name change *)
saveAsMyEd: (#
do (if ('Select a Destination File Name: ',title) ->
myFile.PutFile // true then saveMyEd;
myFile.entry.path.name -> title
if)
#);
(* close and (optionally) save the current text editor *)
close::< (#
do (if docChanged // true then saveAsMyEd if)
#);#); #);
(* === File Menu Patterns === *)
FileMenu::< (#
(* File/Open command *)
iOpen::< (# hit::< (#
myWindow:^edWindow;
do &edWindow[] -> myWindow[];
(if myWindow.openFile // true then
myWindow.open
if)
#);#);
(* File/New command *)
iNew::< (# hit::< (#
myWindow:^edWindow
do &edWindow[] -> myWindow[];
myWindow.newFile;
myWindow.open
#);#);#);
(* === Program start - create the font menu, put it on the ===
* === menu bar, and set the first window tiling position. === *)
nextPosition: @Point;
do FontMenu.Open;
FontMenu.disable;
FontMenu[] -> Menubar.Append;
(5,40) -> nextPosition
#)
---]]
Example 1:
<Pattern Name>: (#
<Attribute 1> ;
<Attribute 2> ;
.
<Attribute N> ;
Enter <Input list>
Do <Imperatives>
Exit <Output list>
#);
Example 2:
Account: (# (* a simple bank account class pattern *)
balance: @integer; (* bank account balance *)
Deposit: (# (* add `amount' to balance *)
amount: @integer(* local declaration *)
enter amount(* input list *)
do balance + amount -> balance (* action *)
exit balance(* output list *)
#);
Withdraw: (#(* subtract `amount' from balance *)
amount: @integer
enter amount
do balance - amount -> balance
exit amount
#); #);
Example 3:
(# (* a program pattern with no name *)
Account: (# (* a pattern declaration within the unnamed pattern *)
acct_balance: @ integer;
Deposit: (#
amount: @ integer
enter amount
do acct_balance + amount -> acct_balance
exit balance
#);
Withdraw: (#
amount: @ integer
enter amount
do acct_balance - amount -> acct_balance
exit amount
#);
Balance: (#
exit acct_balance
#); #);
A: @ Account;
cash_on_hand, balance: @ integer;
do 100 -> &A.Deposit;
250 -> &A.Deposit;
75 -> &A.Withdraw -> cash_on_hand; (* $75 on hand *)
&A.Balance -> balance; (* $275 balance *)
#)
Example 4:
(a)
<Name>: [size] @<Type> ;(* static repetition *)
<Name>: [size] ^<Type> ;(* dynamic repetition *)
(b)
(for <Index>: <Range> repeat <Imperative-list> for)
(c)
(if E0
// E1 then I1
// E2 then I2
. . .
//En then In
else I
if)
Example 5:
(#
Power: (# (* compute X^n where n > 0 *)
X, Y: @ real; n: @ integer;
enter (X, n)
do 1 -> Y;
(for i: n repeat Y * X -> Y for)
exit Y
#)
Reciproc: (# (* compute (Q, 1/Q) *)
Q, R: @ real;
enter Q
do (if (Q // 0) then 0 -> R
else (1 div Q) -> R
if )
exit (Q, R)
#);
A, B: @ real;
do (3.14, 2) -> &Power -> &Reciproc -> (A, B);
(* A = 3.14 ^ 2, B = 1/A *)
#)
Example 6:
Reservation:(#
Date: @DateType;
Customer: ^CustomerRecord
#)
FlightReservation: Reservation (#
FlightNo: ^Flight;
SeatNo: ^Seat
#)
TrainReservation: Reservation (#
TrainNo:^Train;
CarriageNo: ^Carriage;
SeatNo: ^Seat
#)
Example 7:
ReservationRegister: (#
Table: [100] ^ Reservation; (* arbitrary size restriction *)
top: @integer;
Insert: (# (* insert a reservation *)
R: ^ Reservation;
enter R []
do R [] -> Table [top+1 -> top] []
#);
NoOfRes: (# (* get number of reservations in system *)
exit top
#)
GetRes: (# (* get reservation number inx *)
inx: @ integer
enter inx
exit Table [inx] [];
#); #)
Example 8:
(a)
P1: (# a: @integer do 1 -> a #);
P2: P1 (# b: @integer do 2 -> b #);
P3: P2 (# c: @integer do 3 -> c #);
(b)
P3 invoked P2 invoked P1 invoked 1 -> a 2 -> b 3 -> c
(c)
P1: (# a: @ integer do INNER P1;1 -> a #);
P2: P1 (# b: @ integer do 2 -> b; INNER P2#);
P3: P2 (# c: @ integer do 3 -> c #);
(d)
P3 P2 P1 P2 (via INNER P1) 2 -> b
P3 (via INNER P2) 3 -> c 1 -> a
Example 9: Building a virtual pattern
Reservation: (#
Date: @ DateType;
Customer: ^ CustomerRecord
Display: (#
do Date.Display;
Customer.Display; INNER
#); #)
FlightReservation: Reservation (#
FlightNo: ^ Flight;
SeatNo: ^ Seat
Display: (#
do FlightNo.Display;
Customer.Display
#); #)
TrainReservation: Reservation (#
TrainNo: ^Train;
CarriageNo: ^Carriage;
SeatNo: ^ Seat
Display: (#
do TrainNo.Display;
CarriageNo.Display;
SeatNo.Display
#); #)
Example 10:
Reservation: (#
Date: @DateType; Customer ^CustomerRecord
Display:< (#
do Date.Display;
Customer.Display; inner
#); #)
FlightReservation: Reservation (#
FlightNo: ^Flight;
SeatNo: ^Seat
Display::< (#
do FlightNo.Display;
Customer.Display
#); #)
TrainReservation: Reservation (#
TrainNo: ^Train;
CarriageNo: ^Carriage;
SeatNo: ^Seat
Display::< (#
do TrainNo.Display;
CarriageNo.Display;
SeatNo.Display
#); #)